package xyz.renyumeng.xkm.shop.listener;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import xyz.renyumeng.xkm.model.order.OrderInfo;
import xyz.renyumeng.xkm.result.Result;
import xyz.renyumeng.xkm.shop.service.UserProductService;

import javax.annotation.Resource;

import static xyz.renyumeng.xkm.rabbitMQ.RabbitMqConstant.*;

/**
 * @author renyumeng
 * @date Created in 20:27 2023/7/6
 */

@Slf4j
@Component
public class OrderServiceListener {
    @Resource
    private UserProductService userProductService;


    @RabbitListener(
            bindings = @QueueBinding(
                    exchange = @Exchange(name = PRODUCT_EXCHANGE),
                    value = @Queue(PRODUCT_QUEUE),
                    key = PRODUCT_DE_LIST_ROUTING_KEY
            )
    )
    public void deListListener(OrderInfo orderInfo) {
        Long productId = orderInfo.getProductId();
        Result<Object> result = userProductService.listedProduct(productId, 1);
        if (ObjectUtil.equal(result.getCode(), 200)) {
            log.info("用户订单{},下架成功", productId);

        } else {
            log.info("用户订单{},下架失败", productId);
        }
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    exchange = @Exchange(name = PRODUCT_EXCHANGE),
                    value = @Queue(PRODUCT_QUEUE),
                    key = PRODUCT_LIST_ROUTING_KEY
            )
    )
    public void listListener(OrderInfo orderInfo) {
        Long productId = orderInfo.getProductId();
        Result<Object> result = userProductService.listedProduct(productId, 0);
        if (ObjectUtil.equal(result.getCode(), 200)) {
            log.info("用户订单{},上架成功", productId);
        } else {
            log.info("用户订单{},上架失败", productId);
        }
    }
}
